const initialState = {
    table_data:[],
    copy_data:[],
    slice_data:[],
    pageSize:10,
    cur : 1
}

export default (state = initialState, { type, payload }) => {
    let newState = JSON.parse(JSON.stringify(state))
    switch (type) {
        //请求数据
        case "GET_TABLE":
            newState.table_data = payload
            newState.copy_data = payload
            newState.slice_data = payload.slice(newState.cur-1,newState.pageSize)
            return newState
        //获取页码 和当前页数
        case "SET_PAGESIZE":
            newState.pageSize = payload.size
            newState.cur = payload.cur
            return newState
        //删除数据
        case "DEL_TABLEDATA":
            newState.table_data.forEach((item,index) => {
                if(item.id === payload.id){
                    newState.table_data.splice(index,1)
                    newState.slice_data = newState.table_data.slice((newState.cur-1)*newState.pageSize,newState.cur*newState.pageSize)
                }
            })
            if (newState.slice_data.length<1) {
                newState.cur --
                newState.slice_data = newState.table_data.slice((newState.cur-1)*newState.pageSize,newState.cur*newState.pageSize)
            }
            return newState
        //点击分页器 切换数据
        case "SET_SLICE_DATA":
            if (newState.pageSize === payload.pageSize) {
                newState.cur = payload.page
                newState.slice_data = newState.table_data.slice((payload.page-1)*payload.pageSize,payload.page*payload.pageSize)
            }
            
            return newState
        //筛选数据
        case "FILTER_DATA":
            if (newState.table_data.length<50) {   
                newState.table_data = newState.copy_data
                let data = newState.table_data.filter((item) =>item.state === payload)
                newState.table_data = data
            }else{
                let data = newState.table_data.filter((item) =>item.state === payload)
                newState.table_data = data
            }
            newState.slice_data = newState.table_data.slice(newState.cur-1,newState.pageSize)
            return newState
        //添加数据
        case "ADD_TABLEDATA":
            newState.table_data.push(payload)
            newState.copy_data.push(payload)
            return newState
        
        //编辑数据
        case "EDIT_TABLEDATA":
            newState.table_data.forEach((item,index)=>{
                if (item.id === payload.id) {
                    item.key = payload.key
                    item.name = payload.name
                    item.sex = payload.sex
                    item.city = payload.city
                    item.state = payload.state
                }
                newState.copy_data[index]={
                    ...payload
                }
                // newState.copy_data[index].key = payload.key
                // newState.copy_data[index].name = payload.name
                // newState.copy_data[index].sex = payload.sex
                // newState.copy_data[index].city = payload.city
                // newState.copy_data[index].state = payload.state
            })
            newState.slice_data = newState.table_data.slice((newState.cur-1)*newState.pageSize,newState.cur*newState.pageSize)
            return newState
        //从低到高
        case "LOW_HIGH_DATA":
            let data = newState.table_data.sort((a,b)=>{
                return a.key-b.key
            })
            newState.table_data = data
            newState.slice_data = newState.table_data.slice((newState.cur-1)*newState.pageSize,newState.cur*newState.pageSize)
            return newState
            
        //从高到低
        case "HIGH_LOW_DATA":
            const data1 = newState.table_data.sort((a,b)=>{
                return b.key-a.key
            })
            newState.table_data = data1
            newState.slice_data = newState.table_data.slice((newState.cur-1)*newState.pageSize,newState.cur*newState.pageSize)
            return newState
        //搜索
        case "SEACH_DATA":
            if (payload === "") {
                newState.table_data =newState.copy_data
            }else{
                let filter_data = newState.table_data.filter(item=>item.name.includes(payload)||item.city.includes(payload)||item.sex.includes(payload))
                newState.table_data = filter_data
            }
            newState.slice_data = newState.table_data.slice((newState.cur-1)*newState.pageSize,newState.cur*newState.pageSize)
            
            return newState
        default:
            return newState
    }
}
